/*
use "C:\Users\Michelacci\Dropbox\CreditFlows\Data\pwt1001.dta", clear
*csh_i: Gross Capital Formation over GDP: 24.5%
sum  csh_i if countrycode=="ITA" & year>=2009 & year<=2015
*Share in the South 0.238= 0.245* 1/ (1.04 * 1.05* (0.73)+ (1-0.15) *(1-0.73))
*Share in the North 0.249= 0.239*1.04
*/

set more off   
set scheme s1color

use "$data/calibrationdataset.dta", clear

gen R_cl_ori_cl=R_cl_ori*cl_ori 
gen R_tl_st_ori_st=R_tl_st_ori*st_tl_ori  
gen R_tl_mt_ori_mt= R_tl_mt_ori*mt_tl_ori 
gen R_tl_lt_ori_lt=R_tl_lt_ori*lt_tl_or 
*gen R_tl_lt_res_lt=R_tl_lt_res*lt_tl_res

egen Mlatitude=mean(latitude)

/*
CODICE VINCENZO
keep if anno>=2007 & anno<=2015

gen south=(regione_istat>=13 & regione_istat<.)


 collapse(sum)  va2_ce_age* DEBT_ce_age*, by(south)

reshape long va2_ce_age@ DEBT_ce_age@, i(south) j(eta)


 gen lev=DEBT/va2



*/

*egen MGDP=mean(GDP), by(provincia)
gen SD_shockGDP= SD_shock*GDP
*gen SD_shockMGDP= SD_shock*MGDP 
*gen SD_shockpop= SD_shock*pop

*keep if year==2007
/*
gen va_size_ce_age13=va_ce_age13/(nfirms_ce_age13-nfirms_ce_ex_age13)
gen va_size_ce_age11=va_ce_age11/(nfirms_ce_age11-nfirms_ce_ex_age11)
gen prova=(log(va_size_ce_age13)-log(va_size_ce_age11))/2
sum prova if South==1
sum prova if South==0
stop
*/
collapse (sum)  nfirms_ce_ex nfirms_ce dip_ce pop emp GDP  SD_shockGDP ///
g_st_tl_res g_mt_lt_tl_res  g_cl_res st_tl_res  mt_lt_tl_res  cl_res g_lt_tl_ori ///
R_cl_ori_cl R_tl_st_ori_st R_tl_mt_ori_mt R_tl_lt_ori_lt cl_ori st_tl_ori mt_tl_ori lt_tl_ori  ///
   nfirms_age0 nfirms_age1 nfirms_age2 nfirms_age3 nfirms_age4 nfirms_age5 nfirms_age6 nfirms_age7 nfirms_age8 nfirms_age9 nfirms_age10 nfirms_age11 nfirms_age12 nfirms_age13 nfirms_age14 nfirms_age15 nfirms_age16 nfirms_age17 ///
  nfirms_ex_age0 nfirms_ex_age1 nfirms_ex_age2 nfirms_ex_age3 nfirms_ex_age4 nfirms_ex_age5 nfirms_ex_age6 nfirms_ex_age7 nfirms_ex_age8 nfirms_ex_age9 nfirms_ex_age10 nfirms_ex_age11 nfirms_ex_age12 nfirms_ex_age13 nfirms_ex_age14 nfirms_ex_age15 nfirms_ex_age16  nfirms_ex_age17 ///
  nfirms_pg_age0 nfirms_pg_age1 nfirms_pg_age2 nfirms_pg_age3 nfirms_pg_age4 nfirms_pg_age5 nfirms_pg_age6 nfirms_pg_age7 nfirms_pg_age8 nfirms_pg_age9 nfirms_pg_age10 nfirms_pg_age11 nfirms_pg_age12 nfirms_pg_age13 nfirms_pg_age14 nfirms_pg_age15 nfirms_pg_age16 nfirms_pg_age17 ///
  nfirms_pg_ex_age0 nfirms_pg_ex_age1 nfirms_pg_ex_age2 nfirms_pg_ex_age3 nfirms_pg_ex_age4 nfirms_pg_ex_age5 nfirms_pg_ex_age6 nfirms_pg_ex_age7 nfirms_pg_ex_age8 nfirms_pg_ex_age9 nfirms_pg_ex_age10 nfirms_pg_ex_age11 nfirms_pg_ex_age12 nfirms_pg_ex_age13 nfirms_pg_ex_age14 nfirms_pg_ex_age15  nfirms_pg_ex_age16 nfirms_pg_ex_age17 ///
 nfirms_pg_ins_age0 nfirms_pg_ins_age1 nfirms_pg_ins_age2 nfirms_pg_ins_age3 nfirms_pg_ins_age4 nfirms_pg_ins_age5 nfirms_pg_ins_age6 nfirms_pg_ins_age7 nfirms_pg_ins_age8 nfirms_pg_ins_age9 nfirms_pg_ins_age10 nfirms_pg_ins_age11 nfirms_pg_ins_age12 nfirms_pg_ins_age13 nfirms_pg_ins_age14 nfirms_pg_ins_age15 nfirms_pg_ins_age16 nfirms_pg_ins_age17 ///
 nfirms_ce_age0     nfirms_ce_age1     nfirms_ce_age2     nfirms_ce_age3     nfirms_ce_age4     nfirms_ce_age5     nfirms_ce_age6     nfirms_ce_age7     nfirms_ce_age8     nfirms_ce_age9     nfirms_ce_age10     nfirms_ce_age11     nfirms_ce_age12     nfirms_ce_age13     nfirms_ce_age14     nfirms_ce_age15     nfirms_ce_age16     nfirms_ce_age17 ///
 nfirms_ce_ex_age0  nfirms_ce_ex_age1  nfirms_ce_ex_age2  nfirms_ce_ex_age3  nfirms_ce_ex_age4  nfirms_ce_ex_age5  nfirms_ce_ex_age6  nfirms_ce_ex_age7  nfirms_ce_ex_age8  nfirms_ce_ex_age9  nfirms_ce_ex_age10  nfirms_ce_ex_age11  nfirms_ce_ex_age12  nfirms_ce_ex_age13  nfirms_ce_ex_age14  nfirms_ce_ex_age15  nfirms_ce_ex_age16  nfirms_ce_ex_age17 ///
nfirms_ce_ins_age0 nfirms_ce_ins_age1 nfirms_ce_ins_age2 nfirms_ce_ins_age3 nfirms_ce_ins_age4 nfirms_ce_ins_age5 nfirms_ce_ins_age6 nfirms_ce_ins_age7 nfirms_ce_ins_age8 nfirms_ce_ins_age9 nfirms_ce_ins_age10 nfirms_ce_ins_age11 nfirms_ce_ins_age12 nfirms_ce_ins_age13 nfirms_ce_ins_age14 nfirms_ce_ins_age15 nfirms_ce_ins_age16 nfirms_ce_ins_age17 ///
dip_ce_age0        dip_ce_age1        dip_ce_age2        dip_ce_age3        dip_ce_age4        dip_ce_age5        dip_ce_age6        dip_ce_age7        dip_ce_age8        dip_ce_age9        dip_ce_age10        dip_ce_age11        dip_ce_age12        dip_ce_age13        dip_ce_age14        dip_ce_age15        dip_ce_age16        dip_ce_age17 ///
debt_ce_age0       debt_ce_age1       debt_ce_age2       debt_ce_age3       debt_ce_age4       debt_ce_age5       debt_ce_age6       debt_ce_age7       debt_ce_age8       debt_ce_age9       debt_ce_age10       debt_ce_age11       debt_ce_age12       debt_ce_age13       debt_ce_age14       debt_ce_age15       debt_ce_age16       debt_ce_age17 ///
va_ce_age0         va_ce_age1         va_ce_age2         va_ce_age3         va_ce_age4         va_ce_age5         va_ce_age6         va_ce_age7         va_ce_age8         va_ce_age9         va_ce_age10         va_ce_age11         va_ce_age12         va_ce_age13         va_ce_age14         va_ce_age15         va_ce_age16         va_ce_age17 ///
DEBT_ce_age0       DEBT_ce_age1       DEBT_ce_age2       DEBT_ce_age3       DEBT_ce_age4       DEBT_ce_age5       DEBT_ce_age6       DEBT_ce_age7       DEBT_ce_age8       DEBT_ce_age9       DEBT_ce_age10       DEBT_ce_age11       DEBT_ce_age12       DEBT_ce_age13       DEBT_ce_age14       DEBT_ce_age15       DEBT_ce_age16       DEBT_ce_age17 ///
R_ce_age0          R_ce_age1          R_ce_age2          R_ce_age3          R_ce_age4          R_ce_age5          R_ce_age6          R_ce_age7          R_ce_age8          R_ce_age9          R_ce_age10          R_ce_age11          R_ce_age12          R_ce_age13          R_ce_age14          R_ce_age15          R_ce_age16          R_ce_age17    ///
R_cl_ce_age0       R_cl_ce_age1       R_cl_ce_age2       R_cl_ce_age3       R_cl_ce_age4       R_cl_ce_age5       R_cl_ce_age6       R_cl_ce_age7       R_cl_ce_age8       R_cl_ce_age9       R_cl_ce_age10       R_cl_ce_age11       R_cl_ce_age12       R_cl_ce_age13       R_cl_ce_age14       R_cl_ce_age15       R_cl_ce_age16       R_cl_ce_age17 ///
 (mean) R_tl_lt_res year latitude Mlatitude it_growth16V2 growth16V2 leverage16V2 it_leverage16V2   provIstat HICP3  firm_pc firm_size  BCrate_ce SD_lnemp_ce_age0 SD_lndebt_emp_ce_age0 MSD_lndebt_emp_ce_age0, by(South)
 ********************************************

 *sum growth16V2 it_growth16V2 leverage16V2 it_leverage16V2
 replace growth16V2 =(1+growth16V2)^(3)
 replace it_growth16V2=(1+it_growth16V2)^(3)
 sum growth16V2 it_growth16V2 

 
 gen SD_shock=SD_shockGDP/GDP
 
 
 *Normalize GDP per capita
 egen TGDP=total(GDP)
 egen TSD_shockGDP=total(SD_shockGDP)
 egen Temp=total(emp)
 egen Tpop=total(pop)
gen GDP_PW=GDP/emp/(TGDP/Temp)
gen GDP_PC=GDP/pop/(TGDP/Tpop)



gen it_SD_shock=TSD_shockGDP/TGDP

************************************************
* Generate DEBT Guarantee

gen sharedebt_g_mt=g_mt_lt_tl_res/mt_lt_tl_res 
gen sharedebt_g_lt=g_lt_tl_ori/lt_tl_ori
gen sharedebt_g_n1=(g_st_tl_res+ g_mt_lt_tl_res + g_cl_res)/(st_tl_res+ mt_lt_tl_res + cl_res)
gen sharedebt_ng=1-sharedebt_g_n1
label variable sharedebt_ng "Share of non collateralized debt" 
***********************************************************
*Gneerate average interest rates

gen R_tl_av=(R_cl_ori_cl+R_tl_st_ori_st +R_tl_mt_ori_mt+R_tl_lt_ori_lt)/(cl_ori+st_tl_ori+mt_tl_ori+lt_tl_ori)
gen R_cl=R_cl_ori_cl/cl_ori
gen R_st=R_tl_st_ori_st/st_tl_ori
gen R_mt=R_tl_mt_ori_mt/mt_tl_ori
gen R_lt=R_tl_lt_ori_lt/lt_tl_ori
gen R_lt2=R_tl_lt_res
replace R_lt=R_lt2

label variable R_cl "Interest rates credit lines (incl. fees)"
label variable R_tl_av "Interest rate  term loans (incl. fees)"

label variable GDP_PC "GDP per capita"
*label variable erate "Employment rate"
label variable SD_shock "Standard Deviation shock"
*label variable NFC_FDI "Loans-GDP ratio Non-financial-corporations" 
label variable provIstat "Province ISTAT code"






************************************************
*Calculate rate
gen size0=0.5*(dip_ce_age0/(nfirms_ce_age0-nfirms_ce_ex_age0))+0.5*(dip_ce_age1/(nfirms_ce_age1-nfirms_ce_ex_age1))
gen sizeva0=0.5*(va_ce_age0/(nfirms_ce_age0-nfirms_ce_ex_age0))+0.5*(va_ce_age1/(nfirms_ce_age1-nfirms_ce_ex_age1))
foreach num in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17{
*Exit rate VARIABLE FROM CERVED
gen exit_ce_ex_age`num'=nfirms_ce_ex_age`num'/nfirms_ce_age`num'
gen exit_ce_ins_age`num'=nfirms_ce_ins_age`num'/nfirms_ce_age`num'
*Productivity
gen va_e_ce_age`num'=(va_ce_age`num'/dip_ce_age`num')/(va_ce_age0/dip_ce_age0)
*Debt over value added
gen debt_va_ce_age`num'=debt_ce_age`num'/va_ce_age`num'
gen DEBT_va_ce_age`num'=DEBT_ce_age`num'/va_ce_age`num'
*Debt per employee
gen debt_e_ce_age`num'=debt_ce_age`num'/dip_ce_age`num'
gen DEBT_e_ce_age`num'=DEBT_ce_age`num'/dip_ce_age`num'
*Employment size of firms in North and South
*gen e_ce_age`num'=(dip_ce_age`num'/nfirms_ce_age`num')/(dip_ce_age0/nfirms_ce_age0)
gen e_ce_age`num'=(dip_ce_age`num'/(nfirms_ce_age`num'-nfirms_ce_ex_age`num'))/size0
gen va_size_ce_age`num'=(va_ce_age`num'/(nfirms_ce_age`num'-nfirms_ce_ex_age`num'))/sizeva0
*gen va_size_ce_age`num'=(va_ce_age`num'/(nfirms_ce_age`num'-0.5*nfirms_ce_ex_age`num'))/sizeva0
}

replace  e_ce_age0=1
replace  va_size_ce_age0=1






gen Size_ce=(dip_ce_age0+dip_ce_age1+dip_ce_age2+ dip_ce_age3+dip_ce_age4+dip_ce_age5+ dip_ce_age6 + dip_ce_age7+ dip_ce_age8+dip_ce_age9+dip_ce_age10+dip_ce_age11+ dip_ce_age12+ dip_ce_age13+dip_ce_age14+ dip_ce_age15+ dip_ce_age16+dip_ce_age17) ///
/(nfirms_ce_age0+nfirms_ce_age1+nfirms_ce_age2+ nfirms_ce_age3+nfirms_ce_age4+nfirms_ce_age5 + nfirms_ce_age6+nfirms_ce_age7+nfirms_ce_age8+nfirms_ce_age9+nfirms_ce_age10+ nfirms_ce_age11+nfirms_ce_age12+ nfirms_ce_age13+nfirms_ce_age14+nfirms_ce_age15+nfirms_ce_age16+nfirms_ce_age17)

gen nfirm_ce_pc=1/Size_ce




***************************************************************************************************
* Data set for FIRM LIFE CYCLE in North and South: average period 2007-2015




*CALCULATE STATISTICS
*Exit Rate
gen nfirm_ce_ex=(nfirms_ce_ex_age0+nfirms_ce_ex_age1+nfirms_ce_ex_age2+nfirms_ce_ex_age3+nfirms_ce_ex_age4+nfirms_ce_ex_age5+nfirms_ce_ex_age6+nfirms_ce_ex_age7+nfirms_ce_ex_age8+nfirms_ce_ex_age9+nfirms_ce_ex_age10+nfirms_ce_ex_age11+nfirms_ce_ex_age12+nfirms_ce_ex_age13+nfirms_ce_ex_age14+nfirms_ce_ex_age15+nfirms_ce_ex_age16+nfirms_ce_ex_age17)
gen nfirm_ce_2=(nfirms_ce_age0+nfirms_ce_age1+nfirms_ce_age2+nfirms_ce_age3+nfirms_ce_age4+nfirms_ce_age5+nfirms_ce_age6+nfirms_ce_age7+nfirms_ce_age8+nfirms_ce_age9+nfirms_ce_age10+nfirms_ce_age11+nfirms_ce_age12+nfirms_ce_age13+nfirms_ce_age14+nfirms_ce_age15+nfirms_ce_age16+nfirms_ce_age17)
egen Tnfirm_ce_ex=total(nfirm_ce_ex)
egen Tnfirm_ce_2=total(nfirm_ce_2)
gen exitrate_ce=nfirm_ce_ex/nfirm_ce_2
gen It_exitrate_ce=Tnfirm_ce_ex/Tnfirm_ce_2

*BANKRUPTCY RATWE
gen nfirm_ce_ins=(nfirms_ce_ins_age0+nfirms_ce_ins_age1+nfirms_ce_ins_age2+nfirms_ce_ins_age3+nfirms_ce_ins_age4+nfirms_ce_ins_age5+nfirms_ce_ins_age6+nfirms_ce_ins_age7+nfirms_ce_ins_age8+nfirms_ce_ins_age9+nfirms_ce_ins_age10+nfirms_ce_ins_age11+nfirms_ce_ins_age12+nfirms_ce_ins_age13+nfirms_ce_ins_age14+nfirms_ce_ins_age15+nfirms_ce_ins_age16+nfirms_ce_ins_age17)

gen bankrate_ce=nfirm_ce_ins/nfirm_ce_2
egen Tnfirm_ce_ins=total(nfirm_ce_ins)
gen It_bankrate_ce=Tnfirm_ce_ins/Tnfirm_ce_2


*Business Creation rate
replace BCrate_ce=nfirms_ce_age0/dip_ce*100
egen Tnfirms_ce_age0=total(nfirms_ce_age0)
egen Tdip_ce=total(dip_ce)
gen It_BCrate_ce=Tnfirms_ce_age0/Tdip_ce*100


*Number of firms
replace nfirm_ce_pc=nfirms_ce/dip_ce
gen It_nfirm_ce_pc=Tnfirm_ce_2/Tdip_ce






*Leverage ratio
gen total_Debt=DEBT_ce_age0+DEBT_ce_age1+DEBT_ce_age2+DEBT_ce_age3+DEBT_ce_age4+DEBT_ce_age5+DEBT_ce_age6+DEBT_ce_age7+DEBT_ce_age8+DEBT_ce_age9+DEBT_ce_age10+DEBT_ce_age11+DEBT_ce_age12+DEBT_ce_age13+DEBT_ce_age14+DEBT_ce_age15+DEBT_ce_age16+DEBT_ce_age17
gen total_va=va_ce_age0+va_ce_age1+va_ce_age2+va_ce_age3+va_ce_age4+va_ce_age5+va_ce_age6+va_ce_age7+va_ce_age8+va_ce_age9+va_ce_age10+va_ce_age11+va_ce_age12+va_ce_age13+va_ce_age14+va_ce_age15+va_ce_age16+va_ce_age17
egen Ttotal_Debt=total(total_Debt)
egen Ttotal_va=total(total_va)

gen total_DEBT_va_ce=total_Debt/total_va
gen It_total_DEBT_va_ce=Ttotal_Debt/Ttotal_va

gen total_debt_va_ce=(debt_ce_age0+debt_ce_age1+debt_ce_age2+debt_ce_age3+debt_ce_age4+debt_ce_age5+debt_ce_age6+debt_ce_age7+debt_ce_age8+debt_ce_age9+debt_ce_age10+debt_ce_age11+debt_ce_age12+debt_ce_age13+debt_ce_age14+debt_ce_age15+debt_ce_age16+debt_ce_age17) ///
/(va_ce_age0+va_ce_age1+va_ce_age2+va_ce_age3+va_ce_age4+va_ce_age5+va_ce_age6+va_ce_age7+va_ce_age8+va_ce_age9+va_ce_age10+va_ce_age11+va_ce_age12+va_ce_age13+va_ce_age14+va_ce_age15+va_ce_age16+va_ce_age17)


*Relative employment size in Italy
*foreach var in  dip_ce_age10 nfirms_ce_age10 nfirms_ce_ex_age10 dip_ce_age0 nfirms_ce_ex_age0 dip_ce_age1 nfirms_ce_ex_age1 {
*egen T`var'=total(`var')
*}

*egen Tsize0=mean(size0)
*gen Tsize0=Tdip_ce_age0/(Tnfirms_ce_age0-Tnfirms_ce_ex_age0)
*gen It_e_ce_age10=(Tdip_ce_age10/(Tnfirms_ce_age10-Tnfirms_ce_ex_age10))/Tsize0
egen It_e_ce_age10=mean(e_ce_age10)
egen It_e_ce_age15=mean(e_ce_age15)
egen It_va_size_ce_age15=mean(va_size_ce_age15)
*sum It_e_ce_age10

*stop

*gen AVva_e_ce=(va_ce_age0)/(dip_ce_age0)

*ENTRY FLOW of BUSINESSES, per capita
*gen entry_flow=nfirms_entryrate * firm_pc
*gen exit_flow=nfirms_exitrate * firm_pc

*restore BinscatterGDP_PCNFC_FDI.pdf BinscatterGDP_PCGDP_PW.pdf
************************************************************************************
*CALCULATE INTEREST RATES


**********************************************
*Risk Adjusted rate

*gen prova_num=nfirms_ce_ins_age10+nfirms_ce_ins_age11+nfirms_ce_ins_age12+nfirms_ce_ins_age13+nfirms_ce_ins_age14+nfirms_ce_ins_age15+nfirms_ce_ins_age16+nfirms_ce_ins_age17
*gen prova_den=nfirms_ce_age10+nfirms_ce_age11+nfirms_ce_age12+nfirms_ce_age13+nfirms_ce_age14+nfirms_ce_age15+nfirms_ce_age16+nfirms_ce_age17
gen prova_num=nfirms_ce_ins_age11+nfirms_ce_ins_age12+nfirms_ce_ins_age13+nfirms_ce_ins_age14+nfirms_ce_ins_age15+nfirms_ce_ins_age16+nfirms_ce_ins_age17
gen prova_num2=nfirms_ce_ex_age11+nfirms_ce_ex_age12+nfirms_ce_ex_age13+nfirms_ce_ex_age14+nfirms_ce_ex_age15+nfirms_ce_ex_age16+nfirms_ce_ex_age17
gen prova_den=nfirms_ce_age11+nfirms_ce_age12+nfirms_ce_age13+nfirms_ce_age14+nfirms_ce_age15+nfirms_ce_age16+nfirms_ce_age17

gen nfirms_insrate_ceO=prova_num/prova_den
gen nfirms_exrate_ceO=prova_num2/prova_den
*replace sharedebt_g_lt=0
global Adjustguara 0.6
egen Tsharedebt_g_lt=mean(sharedebt_g_lt)
gen M_rbar3=R_lt-nfirms_insrate_ceO*100*(1-sharedebt_g_lt*$Adjustguara)-HICP3

sum  M_rbar3 R_lt nfirms_insrate_ceO sharedebt_g_lt  HICP3



*gen M_rbar3=R_lt-nfirms_insrate_ceO*100*(1-sharedebt_g_n1*$Adjustguara)-HICP3

*Adjusted guarantees
egen It_M_rbar3=mean(M_rbar3)
replace sharedebt_g_lt=sharedebt_g_lt*$Adjustguara
replace sharedebt_g_n1=sharedebt_g_n1*$Adjustguara


foreach var in  g_st_tl_res  g_mt_lt_tl_res  g_cl_res st_tl_res  mt_lt_tl_res  cl_res {
egen T`var'=total(`var')
}
 
gen It_sharedebt_g_n1=(Tg_st_tl_res+ Tg_mt_lt_tl_res + Tg_cl_res)/(Tst_tl_res+ Tmt_lt_tl_res + Tcl_res)*$Adjustguara





*For Italy
egen TR_tl_lt_ori_lt=total(R_tl_lt_ori_lt)
egen Tlt_tl_ori=total(lt_tl_ori)
egen TR_lt=mean(R_lt)

egen Tprova_num=total(prova_num)
egen Tprova_den=total(prova_den)
gen Tnfirms_insrate_ceO=Tprova_num/Tprova_den
*egen Tg_lt_tl_ori=total(g_lt_tl_ori)
*egen Tlt_tl_ori=total(lt_tl_ori)
*gen Tsharedebt_g_lt=Tg_lt_tl_ori/Tlt_tl_ori


*replace Tsharedebt_g_lt=0
*gen It_M_rbar3=TR_lt-Tnfirms_insrate_ceO*100*(1-Tsharedebt_g_n1*$Adjustguara)-HICP3


sum It_M_rbar3 M_rbar3 R_lt nfirms_insrate_ceO

********************************************************************************
*GENERATE Table of Descriptive Statistics for calibration using FILE WRITE
********************************************************************************
*drop if provincia=="."
* ------------------------------------------------------------------------------
* 					Calculate numbers and store as locals
* ------------------------------------------------------------------------------
*Latitude
sum latitude if South==0
local LatNorth : di %4.1f r(mean)
sum latitude if South==1
local LatSouth : di %4.1f r(mean)
sum Mlatitude
local LatItaly:  di %4.1f r(mean)


*1) Population share
replace pop=pop/Tpop
replace emp=emp/Temp
*POPULATION
sum pop if South==0
local PopNorth : di %4.2f r(mean)
sum pop if South==1
local PopSouth : di %4.2f r(mean)
local PopItaly:  di %4.0f 1
*
* EMPLOYMENT
sum emp if South==0
local EmpNorth : di %4.2f r(mean)
sum emp if South==1
local EmpSouth : di %4.2f r(mean)
local EmpItaly:  di %4.0f 1
*

*2) Aggregate labor productivity
*PER CAPITA
*replace GDP_PC=ln(GDP_PC)
sum GDP_PC if South==0
local AprodPCNorth : di %4.2f r(mean)
sum GDP_PC if South==1
local AprodPCSouth : di %4.2f r(mean)
local AprodPCItaly : di %4.0f 1
*
* PER WORKER
*replace GDP_PW=ln(GDP_PW)
sum GDP_PW if South==0
local AprodNorth : di %4.2f r(mean)
sum GDP_PW if South==1
local AprodSouth : di %4.2f r(mean)
local AprodItaly : di %4.0f 0



local InvNorth : di %4.1f 24.8
local InvSouth : di %4.1f 23.7
local InvItaly : di %4.1f 24.5

*4) Exit rate
replace  exitrate_ce=exitrate_ce*100
replace It_exitrate_ce=It_exitrate_ce*100
sum exitrate_ce if South==0
local ExitNorth : di %4.2f r(mean)
sum exitrate_ce if South==1
local ExitSouth : di %4.2f r(mean)
sum It_exitrate_ce
local ExitItaly : di %4.2f r(mean)

*Bankruptcy Rate
*bankrate_ce It_bankrate_ce
replace  bankrate_ce=bankrate_ce*100
replace It_bankrate_ce=It_bankrate_ce*100
sum bankrate_ce if South==0
local BankExitNorth : di %4.2f r(mean)
sum bankrate_ce if South==1
local BankExitSouth : di %4.2f r(mean)
sum It_bankrate_ce
local BankExitItaly : di %4.2f r(mean)



* 5) Business Creation rate
sum BCrate_ce if South==0
local BCNorth : di %4.2f r(mean)
sum BCrate_ce if South==1
local BCSouth : di %4.2f r(mean)
sum It_BCrate_ce
local BCItaly : di %4.2f r(mean)

*6) Number of firms 
sum nfirm_ce_pc if South==0
local NfirmsNorth : di %4.2f r(mean)
sum nfirm_ce_pc if South==1
local NfirmsSouth : di %4.2f r(mean)
sum It_nfirm_ce_pc
local NfirmsItaly : di %4.2f r(mean)

*7) Leverage ratio
sum total_DEBT_va_ce if South==0
local LeverageNorth : di %4.2f r(mean)
sum total_DEBT_va_ce if South==1
local LeverageSouth : di %4.2f r(mean)
sum It_total_DEBT_va_ce
local LeverageItaly : di %4.2f r(mean)


*8) Cost of credit
sum M_rbar3 if South==0
local CostcNorth : di %4.2f r(mean)
sum M_rbar3 if South==1
local CostcSouth : di %4.2f r(mean)
sum It_M_rbar3
local CostcItaly : di %4.2f r(mean)

*9) SD of logged debt per employee
sum SD_lndebt_emp_ce_age0 if South==0
local SDDebtEmpNorth : di %4.2f r(mean)
sum SD_lndebt_emp_ce_age0 if South==1
local SDDebtEmpSouth : di %4.2f r(mean)
sum MSD_lndebt_emp_ce_age0
local SDDebtEmpItaly : di %4.2f r(mean)


*10) Relative employment size: RelativesizeNorth
sum e_ce_age10 if South==0
local RelativesizeNorth : di %4.2f r(mean)
sum e_ce_age10 if South==1
local RelativesizeSouth : di %4.2f r(mean)
sum It_e_ce_age10
local RelativesizeItaly : di %4.2f r(mean)

*10b) Relative employment size: RelativesizeNorth (15 years)
sum e_ce_age15 if South==0
local RelativesizeNorth : di %4.2f r(mean)
sum e_ce_age15 if South==1
local RelativesizeSouth : di %4.2f r(mean)
sum It_e_ce_age15
local RelativesizeItaly : di %4.2f r(mean)

*10c)Relative value added size
replace va_size_ce_age15=log(va_size_ce_age15)
replace It_va_size_ce_age15=log(It_va_size_ce_age15)
sum va_size_ce_age15 if South==0
local RelativesizeVaNorth : di %4.2f r(mean)
sum va_size_ce_age15 if South==1
local RelativesizeVaSouth : di %4.2f r(mean)
sum It_va_size_ce_age15
local RelativesizeVaItaly : di %4.2f r(mean)



*10b) Growth rate 12-14 anni for incumbent firms
*replace growth16V2=growth16V2*100
*replace it_growth16V2=it_growth16V2*100
sum growth16V2 if South==0
local GrowthBalancedNorth : di %4.2f r(mean)
sum growth16V2 if South==1
local GrowthBalancedSouth : di %4.2f r(mean)
sum it_growth16V2
local GrowthBalancedItaly : di %4.2f r(mean)


*10c) Leverage ratio 15 anni for incumbent firms
sum leverage16V2 if South==0
local LeverageBalancedNorth : di %4.2f r(mean)
sum leverage16V2 if South==1
local LeverageBalancedSouth : di %4.2f r(mean)
sum it_leverage16V2
local LeverageBalancedItaly : di %4.2f r(mean)


*11) SD of Shock
sum SD_shock if South==0
local SDshockNorth : di %4.2f r(mean)
sum SD_shock if South==1
local SDshockSouth : di %4.2f r(mean)
sum it_SD_shock
local SDshockItaly : di %4.2f r(mean)


*12)* Debt guarantees
sum sharedebt_g_n1 if South==0
local SharedebtgNorth : di %4.2f r(mean)
sum sharedebt_g_n1 if South==1
local SharedebtgSouth : di %4.2f r(mean)
sum It_sharedebt_g_n1
local SharedebtgItaly : di %4.2f 0.66*`SharedebtgNorth'+(1-0.66)*`SharedebtgSouth'






capture file close sampleTable
	file open 	sampleTable using "$tables/TABLECalibrationNOTE.tex", write replace
	file write 	sampleTable ///
		"\begin{tabular}{lccccccccccccccccccc}" 		_n ///																																													  
	"\hline \\ [-0.25cm]     " 	_n ///
	"                                                            &  (1)                   &&    (2)                  && (3)                      &&  (4)                         &&  (5)                       &&  (6)                        &&    (7)                         &&  (8)                      && (9)                                        \\" _n  ///
	"                                                            &  Population            &&    GDP per              && Latitude                  &&  Business                    && Business                   && N.  firms                   &&    Leverage                    &&  Debt cost                &&  Relative size                            \\" _n  ///
	"                                                            &    share               &&    capita               &&                           &&  exit  ($ \% $)              &&  entry ($ \% $)            && p.c.                        &&      ratio                     &&   $ r_c $ ($ \% $)        &&  at 15 yrs                                \\ [0.2cm]" _n  ///
	"\hline \\[0.1cm]" 	_n ///
	" \multicolumn{1}{l}{\textbf{Italy}}                         &" %4.2f (`PopItaly')   "&&" %4.2f (`AprodPCItaly ')  "&&" %4.1f (`LatItaly ')   "&&" %4.2f (`ExitItaly')        "&&" %4.2f (`BCItaly')        "&&" %4.2f (`NfirmsItaly')     "&&"  %4.2f (`LeverageItaly')     "&&"  %4.2f (`CostcItaly')   "&&"  %4.2f (`RelativesizeVaItaly')           " \\"  _n  /// 		
	" [0.09cm] " 	  _n ///
	" \multicolumn{1}{l}{\textbf{North}}                         &" %4.2f (`PopNorth')   "&&" %4.2f (`AprodPCNorth ')  "&&" %4.1f (`LatNorth ')   "&&" %4.2f (`ExitNorth')        "&&" %4.2f (`BCNorth')        "&&" %4.2f (`NfirmsNorth')     "&&" %4.2f (`LeverageNorth')      "&&"  %4.2f (`CostcNorth')   "&&"  %4.2f (`RelativesizeVaNorth')            " \\"  _n  /// 		
	" [0.09cm] " 	  _n ///
	" \multicolumn{1}{l}{\textbf{South}}                         &" %4.2f (`PopSouth')   "&&" %4.2f (`AprodPCSouth ')  "&&" %4.1f (`LatSouth ')   "&&" %4.2f (`ExitSouth')        "&&" %4.2f (`BCSouth')        "&&" %4.2f (`NfirmsSouth')     "&&" %4.2f (`LeverageSouth')      "&&"  %4.2f (`CostcSouth')   "&&"  %4.2f (`RelativesizeVaSouth')             " \\"  _n  /// 		
	" [0.2cm] \hline" 	  _n ///
	   "\end{tabular}"  _n ///

file close 	sampleTable	
*





capture file close sampleTable
	file open 	sampleTable using "$tables/TABLECalibration.tex", write replace
	file write 	sampleTable ///
		"\begin{tabular}{lccccccccccccccccccccc}" 		_n ///																																													  
	"\hline \\ [-0.25cm]     " 	_n ///
	"                                                            &  (1)                   &&    (2)                                        &&  (3)                     &&  (4)                         &&  (5)                       &&  (6)                        &&    (7)                         &&  (8)                      && (9)                          && (10)                             \\"         _n  ///    
	"                                                            &  Population            &&    GDP per                                    &&  Business                &&  Bankruptcy                  && Business                   && N.  firms                   &&    Leverage                    &&  Debt cost                && SD of idios.                 && Relative emp.                    \\"         _n  ///
	"                                                            &  share                 &&    capita                                     &&  exit,  $ \% $           &&  rate,  $ \% $               &&  entry, $ \% $             && p.c.                        &&      ratio                     &&   $ r_c $, $ \% $         && shocks, $ \sigma $           && size, 11-14 yrs                  \\ [0.2cm]" _n  ///
	"\hline \\[0.1cm]" 	_n ///
	" \multicolumn{1}{l}{\textbf{Italy}}                         &" %4.2f (`PopItaly')   "&&" %4.2f (`AprodPCItaly ')                     "&&" %4.2f (`ExitItaly')    "&&" %4.2f (`BankExitItaly')    "&&" %4.2f (`BCItaly')        "&&" %4.2f (`NfirmsItaly')     "&&"  %4.2f (`LeverageItaly')     "&&"  %4.2f (`CostcItaly')     "&&"  %4.2f (`SDshockItaly')  "&&"  %4.2f (`GrowthBalancedItaly')  " \\"       _n  /// 		
	" [0.09cm] " 	  _n ///
	" \multicolumn{1}{l}{\textbf{North}}                         &" %4.2f (`PopNorth')   "&&" %4.2f (`AprodPCNorth ')                     "&&" %4.2f (`ExitNorth')    "&&" %4.2f (`BankExitNorth')    "&&" %4.2f (`BCNorth')        "&&" %4.2f (`NfirmsNorth')     "&&" %4.2f (`LeverageNorth')      "&&"  %4.2f (`CostcNorth')     "&&"  %4.2f (`SDshockNorth')  "&&"  %4.2f (`GrowthBalancedNorth') " \\"        _n  /// 		
	" [0.09cm] " 	  _n ///
	" \multicolumn{1}{l}{\textbf{South}}                         &" %4.2f (`PopSouth')   "&&" %4.2f (`AprodPCSouth ')                     "&&" %4.2f (`ExitSouth')    "&&" %4.2f (`BankExitSouth')    "&&" %4.2f (`BCSouth')        "&&" %4.2f (`NfirmsSouth')     "&&" %4.2f (`LeverageSouth')      "&&"  %4.2f (`CostcSouth')     "&&"  %4.2f (`SDshockSouth')  "&&"  %4.2f (`GrowthBalancedSouth') " \\"        _n  /// 		
	" [0.2cm] \hline" 	  _n ///
	   "\end{tabular}"  _n ///

file close 	sampleTable	



***********************************************************************************************************************************

**********************************************************************************
* GRAPH GDP
********************************************************************************** 
 use "$data/calibrationdataset.dta", clear
*Normalize GDP per worker
 *gen GDP_PC=GDP/emp
*Normalize GDP per capita
gen GDP_PC=GDP/pop
 
collapse (mean) GDP pop emp GDP_PC provIstat, by(provincia) 
egen TGDP=total(GDP)
egen Tpop=total(pop)
egen Temp=total(emp)

*replace GDP_PC=GDP/pop/(TGDP/Tpop)
replace GDP_PC=log(GDP_PC)


**************************************************************************************
* GENERATE MAP for GDP per capita
*************************************************************************************

ren provIstat id
sort id
merge 1:1 id using "$data/itprovdb"
capture drop _merge

/* Accounts for the fact that we have aggregated the new provinces  */
gen province_temp=id
 
 
 
 capture drop prova 
capture drop prova2
gen prova=GDP_PC if id==90
egen prova2=max(prova)
replace GDP_PC=prova2 if (id==104 | id==105)
*************************************
*Medio-Carpidana e Carbonia go to Cagliari
capture drop prova prova2
gen prova=GDP_PC if id==92
egen prova2=max(prova)
replace GDP_PC=prova2 if (id==106 | id==107)
*edit urate id
*************************************
*Monza go to Milano
capture drop prova prova2
gen prova=GDP_PC if id==15
egen prova2=max(prova)
replace GDP_PC=prova2 if id==108
*edit GDP_PC id
*************************************
*Fermo go to Ascoli Piceno
capture drop prova prova2
gen prova=GDP_PC if id==44
egen prova2=max(prova)
replace GDP_PC=prova2 if id==109
*edit GDP_PC id
*************************************
*Barletta go to Bari
capture drop prova prova2
gen prova=GDP_PC if id==72
egen prova2=max(prova)
replace GDP_PC=prova2 if id==110
*edit GDP_PC id
*************************************
*Vibo Valenia go to Reggio Calabria
capture drop prova prova2
gen prova=GDP_PC if id==80
egen prova2=max(prova)
replace GDP_PC=prova2 if id==102

/* Provinces to replace
replace province_temp=90 if id==104
replace province_temp=90 if id==105
replace province_temp=92 if id==106
replace province_temp=92 if id==107
replace province_temp=15 if id==108
replace province_temp=44 if id==109
replace province_temp=72 if id==110
*/
sort province_temp
*by province_temp: egen iR2_q_plot=min(iR2_q)
format GDP_PC %4.2f
format GDP_PC %4.1f




*drop if phi_mod==. /* provinces we eliminated because do not have data for all years */
capture drop prova
egen prova=mean(GDP_PC)
replace GDP_PC=GDP_PC-prova
sum GDP_PC 
local MpPC : di %4.2f round(r(mean))
local SDPC : di %4.2f r(sd)

*tmap choropleth phi_mod, id(id) map("$graphs/provcoord.dta") palette(Blues)
spmap GDP_PC using "$data/provcoord.dta", id(id) fcolor(Blues) ndfcolor(red) clmethod(e) clnumber(9) title("Mean `MpPC';  SD `SDPC'", color(blue) size(*1.1) position(6))
*eir(0 1)
graph export "$figures/GDP_PC_graph.pdf", replace as(pdf)
*stop

spmap GDP_PC using "$data/provcoord.dta", id(id) fcolor(Blues) ndfcolor(red) clmethod(e) clnumber(9) 
*eir(0 1)
graph export "$figures/GDP_PC_graph2.pdf", replace as(pdf)
capture drop province_temp prova2 prova
*clear




**********************************************************************************
* GRAPH GDP for NOTE
********************************************************************************** 
 use "$data/calibrationdataset.dta", clear
*Normalize GDP per worker
* gen GDP_PC=GDP/emp
*Normalize GDP per capita
gen GDP_PC=GDP/pop
 
collapse (mean) GDP pop emp GDP_PC provIstat, by(provincia) 
egen TGDP=total(GDP)
egen Tpop=total(pop)
egen Temp=total(emp)

*replace GDP_PC=GDP/pop/(TGDP/Tpop)
replace GDP_PC=log(GDP_PC)


**************************************************************************************
* GENERATE MAP for GDP per capita
*************************************************************************************

ren provIstat id
sort id
merge 1:1 id using "$data/itprovdb"
capture drop _merge

/* Accounts for the fact that we have aggregated the new provinces  */
gen province_temp=id
 
 
 
 capture drop prova 
capture drop prova2
gen prova=GDP_PC if id==90
egen prova2=max(prova)
replace GDP_PC=prova2 if (id==104 | id==105)
*************************************
*Medio-Carpidana e Carbonia go to Cagliari
capture drop prova prova2
gen prova=GDP_PC if id==92
egen prova2=max(prova)
replace GDP_PC=prova2 if (id==106 | id==107)
*edit urate id
*************************************
*Monza go to Milano
capture drop prova prova2
gen prova=GDP_PC if id==15
egen prova2=max(prova)
replace GDP_PC=prova2 if id==108
*edit GDP_PC id
*************************************
*Fermo go to Ascoli Piceno
capture drop prova prova2
gen prova=GDP_PC if id==44
egen prova2=max(prova)
replace GDP_PC=prova2 if id==109
*edit GDP_PC id
*************************************
*Barletta go to Bari
capture drop prova prova2
gen prova=GDP_PC if id==72
egen prova2=max(prova)
replace GDP_PC=prova2 if id==110
*edit GDP_PC id
*************************************
*Vibo Valenia go to Reggio Calabria
capture drop prova prova2
gen prova=GDP_PC if id==80
egen prova2=max(prova)
replace GDP_PC=prova2 if id==102

/* Provinces to replace
replace province_temp=90 if id==104
replace province_temp=90 if id==105
replace province_temp=92 if id==106
replace province_temp=92 if id==107
replace province_temp=15 if id==108
replace province_temp=44 if id==109
replace province_temp=72 if id==110
*/
sort province_temp
*by province_temp: egen iR2_q_plot=min(iR2_q)
format GDP_PC %4.2f
format GDP_PC %4.1f




*drop if phi_mod==. /* provinces we eliminated because do not have data for all years */
capture drop prova
egen prova=mean(GDP_PC)
replace GDP_PC=GDP_PC-prova
sum GDP_PC 
local MpPC : di %4.2f round(r(mean))
local SDPC : di %4.2f r(sd)

*tmap choropleth phi_mod, id(id) map("$graphs/provcoord.dta") palette(Blues)
spmap GDP_PC using "$data/provcoord.dta", id(id) fcolor(Blues) ndfcolor(red) clmethod(e) clnumber(9) title("Mean `MpPC';  SD `SDPC'", color(blue) size(*1.1) position(6))
*eir(0 1)
graph export "$figures/GDP_PC_graphNOTE.pdf", replace as(pdf)
*stop

spmap GDP_PC using "$data/provcoord.dta", id(id) fcolor(Blues) ndfcolor(red) clmethod(e) clnumber(9) 
*eir(0 1)
graph export "$figures/GDP_PC_graphNOTE2.pdf", replace as(pdf)
capture drop province_temp prova2 prova
*clear

